/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : MadMinuteAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 8, 2009  Raghu Sarraff	 		        Created
 */
package net.lwf.web.struts.action.maths;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.handler.entities.math.MadMinScore;
import lwf.handler.entities.math.MadMinSum;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.math.arithmatics.MadMinuteHandler;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.struts.action.LwfWorksheetAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


//public class MadMinuteAction extends DispatchAction {
public class MadMinuteAction extends LwfWorksheetAction {
		
	public ActionForward home(ActionMapping mapping, 
			                  ActionForm form,
							  HttpServletRequest request,
							  HttpServletResponse response)
		 {
		MadMinActionForm mmaf = (MadMinActionForm) form;
		MadMinuteHandler handler = new MadMinuteHandler(getGrade());
		//Set operations per the grade level
		mmaf.setRadioData(handler.getOperationsData());
		
		mmaf.setOperation("");
		mmaf.setPagedata(null);
		return mapping.findForward("success");
		
	}
	
	public ActionForward display(ActionMapping mapping,
			                     ActionForm form,
								 HttpServletRequest request,
								 HttpServletResponse response)
		throws Exception {
		
		

		MadMinActionForm mmaf = (MadMinActionForm) form;
		String operation = request.getParameter("operation");
		if(!operation.equalsIgnoreCase("quickmath")) {
			mmaf.setOperation(operation);
		}
		MadMinuteHandler handler = (MadMinuteHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		@SuppressWarnings("unchecked")
		List<MadMinSum> pagedata = (List<MadMinSum>) qinfo.getQuestionObject();
		mmaf.setPagedata(pagedata);
		//Set operations per the grade level
		mmaf.setRadioData(handler.getOperationsData());
		return mapping.findForward("success");

	}
	
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		MadMinActionForm mmaf = (MadMinActionForm) form;
		List<MadMinSum> sumdata = mmaf.getPagedata();
		this.processAnswers(request, sumdata);
		MadMinScore score = new MadMinScore(sumdata);
		mmaf.setScore(score);
		return mapping.findForward("score");
	}

//	
//	private MadMinuteHandler getHandler(String operation) {
//		return new MadMinuteHandler(operation);
//	}
	
	private void processAnswers(HttpServletRequest request, List<MadMinSum> sumdata) {
		if(sumdata != null && sumdata.size() != 0) {
			for(int i=0; i<sumdata.size(); i++) {
				MadMinSum sum = (MadMinSum) sumdata.get(i);
				if(!sum.getOperation().equals(IHandlerConstant.DIVISION)) {
					String answer = request.getParameter(sum.getAnswerbox());
					sum.setUseranswer(answer);
				}
				else {
					String remainder = request.getParameter(sum.getRemainderbox());
					sum.setUseranswerremainder(remainder);
					String quotient = request.getParameter(sum.getQuotientbox());
					sum.setUseranswerquotient(quotient);
				}
				//sum.print();
				//sum.printUserAnswers();
			}
			
		}
	}

	@Override
	public String getPageTitle() {
		return IPageTitle.MAD_MINUTE_MATH;
	}

	@Override
	public String getMainTitle() {
		return null;
	}

	@Override
	public String getPdfLink() {
		return null;
	}

	@Override
	public String getQuizLink() {
		return null;
	}
	
	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		String operation = getOperation();
		return new MadMinuteHandler(getGrade(), operation);
	}
	
} //MadMinuteAction

